package com.hengxinguotong.hxgtwygsp.ui.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.PageEvent;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseActivity;
import com.hengxinguotong.hxgtwygsp.ui.commonFacility.CommonFacilityActivity;
import com.hengxinguotong.hxgtwygsp.ui.imgView.ImgViewActivity;
import com.hengxinguotong.hxgtwygsp.ui.main.Text.TextFragment;
import com.hengxinguotong.hxgtwygsp.ui.main.picture.PictureFragment;
import com.hengxinguotong.hxgtwygsp.ui.main.video.VideoFragment;
import com.hengxinguotong.hxgtwygsp.ui.newView.PublicRevenueInActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.maintenanceFund.MaintenanceFundActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.outsourceService.OutsourceServiceActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.ownerComplaintEvaluate.OwnerComplainEvaluateActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.periodicReport.PeriodicReportActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.projectLeader.ProjectLeaderActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.propertyFeePayment.PropertyFeePaymentActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.propertyServiceContentAndStandard.PropertyServiceContentAndStandardActivity;
import com.hengxinguotong.hxgtwygsp.ui.newView.propertySuperviseInfo.PropertySuperviseInfoActivity;
import com.hengxinguotong.hxgtwygsp.ui.readFile.FileDisplayActivity;
import com.hengxinguotong.hxgtwygsp.ui.supervisePhone.SupervisePhoneActivity;
import com.hengxinguotong.hxgtwygsp.utils.CommUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements MainContract.IView, ViewPager.OnPageChangeListener
{
    @Inject
    MainPresenter mPresenter;
    @BindView(R.id.main_center_pager)
    ViewPager mainCenterPager;
    @BindView(R.id.main_center_group)
    RadioGroup mainCenterGroup;

    @Inject
    VideoFragment videoFragment;
    @Inject
    PictureFragment pictureFragment;
    @Inject
    TextFragment textFragment;
    List<Fragment> fragmentList;
    MainAdapter mainAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mPresenter.bindView(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId()
    {
        return R.layout.activity_main;
    }

    @Override
    public void init()
    {
        mainCenterPager.setOffscreenPageLimit(3);
        fragmentList = new ArrayList<>();
        fragmentList.add(videoFragment);
        fragmentList.add(pictureFragment);
        fragmentList.add(textFragment);
        mainAdapter = new MainAdapter(getSupportFragmentManager());
        mainCenterPager.setAdapter(mainAdapter);
        RadioButton radioButton;
        int padding = getResources().getDimensionPixelSize(R.dimen.px_10);
        for (Fragment fragment : fragmentList)
        {
            radioButton = new RadioButton(this);
            radioButton.setClickable(false);
            radioButton.setPadding(padding, 0, padding, 0);
            radioButton.setBackgroundColor(Color.TRANSPARENT);
            radioButton.setButtonDrawable(R.drawable.main_radio_bg);
            mainCenterGroup.addView(radioButton);
            mainCenterGroup.check(mainCenterGroup.getChildAt(0).getId());
        }
        mainCenterPager.addOnPageChangeListener(this);
        mainCenterPager.setCurrentItem(1);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {

    }

    @Override
    public void onPageSelected(int position)
    {
        mainCenterGroup.check(mainCenterGroup.getChildAt(position).getId());
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {

    }

    @Subscribe
    public void setCurrentPage(PageEvent pageEvent)
    {
//        int currentItem = mainCenterPager.getCurrentItem();
//        mainCenterPager.setCurrentItem(currentItem == fragmentList.size()-1 ? 0 : currentItem + 1);
    }

    public class MainAdapter extends FragmentPagerAdapter
    {

        public MainAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            return fragmentList.get(position);
        }

        @Override
        public int getCount()
        {
            return fragmentList.size();
        }

//        @Override
//        public int getCount()
//        {
//            return fragmentList == null ? 0 : Integer.MAX_VALUE;
//        }
//
//        @Override
//        public Object instantiateItem(ViewGroup container, int position)
//        {
//            return super.instantiateItem(container, position % fragmentList.size());
//        }
    }

    @OnClick({
            R.id.tv_base_info,R.id.tv_common_part,R.id.tv_common_equipment,R.id.tv_common_facility,
            R.id.ll_project_leader_1,R.id.ll_project_leader_2,R.id.ll_project_leader_3,R.id.ll_project_leader_4,
            R.id.tv_property_company_introduction,R.id.tv_property_service_price,R.id.tv_property_service_base_standard,R.id.tv_property_service_contract,
            R.id.tv_property_charge,R.id.ll_property_charge,R.id.tv_public_revenue_in,R.id.ll_public_revenue_in,
            R.id.tv_outsource_service,R.id.tv_periodic_report,
            R.id.tv_supervise_service_phone,R.id.tv_property_supervise_info, R.id.tv_owner_complaint_evaluate,
            R.id.ll_property_member_1,R.id.ll_property_member_2,R.id.ll_property_member_3,R.id.ll_property_member_4,
            R.id.tv_manage_statute,R.id.tv_owner_meeting_rule,R.id.tv_property_work_rule,R.id.tv_property_work_report,
            R.id.tv_public_revenue_out,R.id.ll_public_revenue_out,R.id.tv_maintenance_fund_situation,R.id.ll_maintenance_fund_situation,
            R.id.tv_owner_property_fee_payment
    })
    public void onTableClick(View v){
        switch (v.getId()) {
            case R.id.tv_base_info:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_community_info),
                        R.mipmap.iv_base_info);
                break;
            case R.id.tv_common_part:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_common_part),
                        R.mipmap.iv_common_part);
                break;
            case R.id.tv_common_equipment:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_common_equipment),
                        R.mipmap.iv_common_equipment);
                break;
            case R.id.tv_common_facility:
                Intent intent = new Intent(this, CommonFacilityActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_project_leader_1:
                intent = new Intent(this, ProjectLeaderActivity.class);
                intent.putExtra("position",0);
                startActivity(intent);
                break;
            case R.id.ll_project_leader_2:
                intent = new Intent(this, ProjectLeaderActivity.class);
                intent.putExtra("position",1);
                startActivity(intent);
                break;
            case R.id.ll_project_leader_3:
                intent = new Intent(this, ProjectLeaderActivity.class);
                intent.putExtra("position",2);
                startActivity(intent);
                break;
            case R.id.ll_project_leader_4:
                intent = new Intent(this, ProjectLeaderActivity.class);
                intent.putExtra("position",3);
                startActivity(intent);
                break;
            case R.id.tv_property_company_introduction:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_property_company_introduction),
                        R.mipmap.iv_property_company_introduce);
                break;
            case R.id.tv_property_service_price:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_property_service_price),
                        R.mipmap.iv_property_service_price);
                break;
            case R.id.tv_property_service_base_standard:
                intent = new Intent(this, PropertyServiceContentAndStandardActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_property_service_contract:
                FileDisplayActivity.show(this, getString(R.string.main_property_service_contract),
                        CommUtil.getDataDir(this) + File.separator + "property_service_contract.pdf");
                break;
            case R.id.ll_property_charge:
            case R.id.tv_property_charge:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_property_charge_situation),
                        R.mipmap.iv_property_charge);
                break;
            case R.id.tv_public_revenue_in:
            case R.id.ll_public_revenue_in:
                intent = new Intent(this, PublicRevenueInActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_outsource_service:
                intent = new Intent(this, OutsourceServiceActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_periodic_report:
                intent = new Intent(this, PeriodicReportActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_supervise_service_phone:
                intent = new Intent(this, SupervisePhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_property_supervise_info:
                intent = new Intent(this, PropertySuperviseInfoActivity.class);
                startActivity(intent);
                break;
            case  R.id.tv_owner_complaint_evaluate:
                intent = new Intent(this, OwnerComplainEvaluateActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_property_member_1:
            case R.id.ll_property_member_2:
            case R.id.ll_property_member_3:
            case R.id.ll_property_member_4:
                ImgViewActivity.startAction(this,
                        "业委会成员信息（第二届）",
                        R.mipmap.iv_property_member);
                break;
            case R.id.tv_manage_statute:
                FileDisplayActivity.show(this, getString(R.string.main_manage_statute),
                        CommUtil.getDataDir(this) + File.separator + "manage_statute.doc");
                break;
            case R.id.tv_owner_meeting_rule:
                FileDisplayActivity.show(this, getString(R.string.main_owner_meeting_rule),
                        CommUtil.getDataDir(this) + File.separator + "owner_meeting_rule.doc");
                break;
            case R.id.tv_property_work_rule:
                FileDisplayActivity.show(this, getString(R.string.main_property_work_rule),
                        CommUtil.getDataDir(this) + File.separator + "property_work_rule.doc");
                break;
            case R.id.tv_property_work_report:
                // 业委会工作报告  为空
                FileDisplayActivity.show(this, getString(R.string.main_property_work_report),
                        CommUtil.getDataDir(this) + File.separator + "null.doc");
                break;
            case R.id.tv_public_revenue_out:
            case R.id.ll_public_revenue_out:
                ImgViewActivity.startAction(this,
                        getString(R.string.main_public_revenue_out),
                        R.mipmap.iv_public_revenue_out);
                break;
            case R.id.tv_maintenance_fund_situation:
            case R.id.ll_maintenance_fund_situation:
                intent = new Intent(this, MaintenanceFundActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_owner_property_fee_payment:
                intent = new Intent(this, PropertyFeePaymentActivity.class);
                startActivity(intent);
                break;
        }
    }
}
